package com.soft.servlet;

import com.soft.dao.HomeDAO;
import com.soft.entity.Information;
import com.soft.entity.Knowledge;
import com.soft.entity.Recruit;
import com.soft.util.PageBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet({"/HomeServlet"})
public class HomeServlet
        extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String uri;

    public void destroy() {
        super.destroy();
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");


        String method = request.getParameter("method");


        if ("knowledge".equals(method)) {

            knowledge(request, response);
        } else if ("news".equals(method)) {

            news(request, response);
        } else if ("recruit".equals(method)) {

            recruit(request, response);
        } else if ("commodity".equals(method)) {

            commodity(request, response);
        } else if ("search_commodity".equals(method)) {

            search_commodity(request, response);
        } else if ("commodity_detail".equals(method)) {

            commodity_detail(request, response);
        } else if ("knowledge_detail".equals(method)) {

            knowledge_detail(request, response);
        } else if ("information_detail".equals(method)) {

            information_detail(request, response);
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    public void commodity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String currPage = request.getParameter("currentPage");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);


            HomeDAO dao = new HomeDAO();
            Map<String, Object> result = dao.getCommodity(pageBean);


            request.setAttribute("result", result);

            this.uri = "/Home/product_display.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void search_commodity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String search_name = request.getParameter("search_name");
            String cids = request.getParameter("cid");

            String currPage = request.getParameter("currentPage");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);
            int cid = Integer.parseInt(cids);


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);


            HomeDAO dao = new HomeDAO();
            Map<String, Object> result = dao.searchCommodity(pageBean, cid, search_name);


            request.setAttribute("result", result);

            this.uri = "/Home/product_display.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void knowledge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String currPage = request.getParameter("currentPage");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);


            HomeDAO dao = new HomeDAO();
            Map<String, Object> result = dao.getKnowledge(pageBean);


            request.setAttribute("result", result);

            this.uri = "/Home/knowledge.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void news(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String currPage = request.getParameter("currentPage");

            if (currPage == null || "".equals(currPage.trim())) {
                currPage = "1";
            }

            int currentPage = Integer.parseInt(currPage);


            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(currentPage);


            HomeDAO dao = new HomeDAO();
            Map<String, Object> result = dao.getNews(pageBean);


            request.setAttribute("result", result);


            this.uri = "/Home/news.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void recruit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            HomeDAO dao = new HomeDAO();
            ArrayList<Recruit> result = dao.getRecruit();


            request.setAttribute("result", result);


            this.uri = "/Home/recruit.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void commodity_detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int commodityId = Integer.parseInt(request.getParameter("id"));


            HomeDAO dao = new HomeDAO();
            Map<String, Object> result = dao.commodityDetail(commodityId);


            request.setAttribute("result", result);

            this.uri = "/Home/goods_detail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void knowledge_detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int KnowledgeId = Integer.parseInt(request.getParameter("id"));


            HomeDAO dao = new HomeDAO();
            Knowledge result = dao.knowledgeDetail(KnowledgeId);


            request.setAttribute("result", result);

            this.uri = "/Home/knowledge_detail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void information_detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int Id = Integer.parseInt(request.getParameter("id"));


            HomeDAO dao = new HomeDAO();
            Information result = dao.informationDetail(Id);


            request.setAttribute("result", result);

            this.uri = "/Home/news_detail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }

    public void init() throws ServletException {
    }
}
